<template>
  <view class="arrived">
    <view class="tabs-nav">
      <u-tabs
        :list="tabsList"
        :activeStyle="activeStyle"
        :inactiveStyle="inactiveStyle"
        :current="current"
        lineColor="#2354E3"
        @click="tabClick"
      ></u-tabs>
      <!-- lineWidth="80" -->
    </view>
    <swiper :current="current" @change="swiperChange" class="shopList">
      <swiper-item v-for="(titem, tindex) in tabsList" :key="tindex">
        <scroll-view class="sl-sv" scroll-y="true" @scrolltolower="scrolower">
          <view class="box" v-if="orderList.length" >
            <view class="item" v-for="(item, index) in orderList" :key="index">
              <view class="title">{{
                current == 4 ? item.track : ''
              }}</view>
              <view class="pack">仓库：{{ item.warehouse }}</view>
			  <!-- <view class="pack">下单时间：{{ item.add_time }}</view> -->
              <view class="time" v-if="current == 1 || item.status==1">预报时间：{{item.add_time}}</view>
			  <view class="time" v-if="current == 2">入库时间：{{item.warehousing_time}}</view>
              <view class="time" v-if="current == 2">在库天数：{{item.day}}</view>
              <view class="time" v-if="current == 3">下单时间： {{item.add_time}}</view>
              <view class="time flex" v-if="current == 3"
                >运单号： {{item.track}}
				<image  class="copy-btn" @click="$utils.copyMessage(item.track)" src="@/static/icon/copy-icon.png" mode="aspectFill"></image>
				</view
              >
			  <!-- （kg）：{{item.weight}} -->
              <view class="time" v-if="current != 1">件数：{{item.weight}}</view>
              <view class="article"
                >物品：
					<text v-for="it in item.info" :key="it.id">
					{{it.name}}{{it.brand?'/'+it.brand:''}}{{it.spe?'/'+it.spe:''}}{{it.num?'/'+it.num:''}}{{it.price?'/'+it.price:''}}；
					 </text>
				</view
              >
			  <view class="time" v-if="current == 2 || current == 3">是否异常：
				<text :style="{color: item.is_new?'red':''}">{{item.is_new==1?'是':'否'}}</text>
			  </view>
              <view class="checkbox" v-if="current == 2">
				  <view class="my-checkbox" @click="changeSelect(item)" :style="{backgroundColor:item.select?'#2979ff':'#fff',borderColor:item.select?'#2979ff':'#c8c9cc'}">
				  	<u-icon name="checkbox-mark" color="#fff" size="25"></u-icon>
				  </view>
                <!-- <u-checkbox-group
                  v-model="itemCheckbox"
                  placement="column"
                  @change="checkboxChange"
                >
                  <u-checkbox
                    :customStyle="{ marginTop: '3px' }"
                    :name="item.id"
                    :iconSize="25"
                    :size="25"
                  >
                  </u-checkbox>
                </u-checkbox-group> -->
              </view>
              <view class="btnbox" v-if="current == 4">
                <view class="btn" @click="detail(item)">详情</view>
                <view
                  class="btn detail"
                  @click="claim(item)"
                  >认领</view
                >
              </view>
              <view class="btnbox" v-else>
				<view class="btn err" style="padding: 15rpx 20rpx;" @click="cancel(item)" v-if="current<2 && item.status<2"
				  >取消</view
				>
				<view class="btn err" style="padding: 15rpx 20rpx;" @click="connection(item)" v-if="(current == 2||current == 3) && item.is_new==1"
				    >联系客服</view
				  >
                <view class="btn" @click="del(item)" v-if="current<3 && item.status<3"
                  >删除</view
                >
                <view class="btn" @click="edit(item)" v-if="current<3 && item.status<3"
                  >编辑</view
                >
                <view class="btn detail" @click="detail(item)">详情</view>
              </view>
            </view>
          </view>
		  <view v-else class="empty" >
		  	暂无数据...
		  </view>
        </scroll-view>
      </swiper-item>
    </swiper>
    <view class="search" v-if="current < 2" @click="open">
      <u-icon name="search" color="#2354E3" size="40"></u-icon>
    </view>
    <!-- 全选 -->
    <view class="bottom space-between" v-if="current == 2 && orderList.length">
      <view class="left row">
       <view class="checked">
          <u-checkbox-group
            v-model="allCheckbox"
            placement="column"
            @change="checkboxAll"
          >
            <u-checkbox
              :customStyle="{ marginTop: '3px' }"
              name="全选"
              :iconSize="25"
              :size="25"
            >
            </u-checkbox>
          </u-checkbox-group>
        </view>
        <view>全选</view>
      </view>
      <view class="right" @click="submit">提交发货</view>
    </view>
    <!-- 搜索 -->
    <u-popup
      :show="popupShow"
      :round="10"
      mode="bottom"
      :closeable="true"
      @close="close"
    >
      <view class="content">
        <view class="ptitle">包裹查找</view>
        <view class="pcontet">
          <u-input
            placeholder="输入包裹追踪号"
            border="none"
            v-model="searchValue"
            :customStyle="inputStyle"
          ></u-input>
          <!-- placeholderStyle="{'font-size': '28rpx';
        'color':' #999999'}" -->
        </view>
        <view class="pbtn space-between">
          <view class="btn btn1" @click="searchValue = ''">重置</view>
          <view class="btn btn2" @click="onSearch">查询</view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 导航栏
      tabsList: [
        {
          name: "所有包裹",
        },
        {
          name: "未到库",
        },
        {
          name: "已入库",
        },
        {
          name: "已下单",
        },
        {
          name: "待认领",
        },
		{
          name: "已取消",
        },
      ],
      current: 0,
      activeStyle: {
        color: "#2354E3",
        fontSize: "32rpx",
        fontWeight: "bold",
      },
      inactiveStyle: {
        color: "#333333",
        fontSize: "32rpx",
      },
      // 订单列表
      orderList: [],
      // 输入框样式
      inputStyle: {
        "background-color": "#F5F5F5",
        "border-radius": "5rpx",
        "padding-left": "43rpx",
        padding: "30rpx",
      },
      // 搜索值
      searchValue: "",
      // 搜索框
      popupShow: false,
      // 商品选择框值
      itemCheckbox: [],
      // 全选
      allCheckbox: [],
	  checkArrData: []
    };
  },

  onLoad(options) {
    if (options.current) {
      this.current = options.current;
    }
	this.getOrderList();
	this.getOrderNum();
  },

  methods: {
	cancel(item){
		console.log(item,"item")
		uni.navigateTo({
			url:`/pages/home/cancelPackage?id=${item.id}`
		})
	},
	changeSelect(item){
		item.select=!item.select  
	},
	resetTabsList(){
	  this.tabsList = [
		{
		  name: "所有包裹",
		},
		{
		  name: "未到库",
		},
		{
		  name: "已入库",
		},
		{
		  name: "已下单",
		},
		{
		  name: "待认领",
		},
		{
		  name: "已取消",
		}
	  ]
	},
	getOrderNum(){
		this.$api.post({
			url: "/ebapi/public_api/reservation_order_count",
			// data: {status:this.current},
			success: (res) => {
				console.log(res,"res");
				this.resetTabsList();
				this.tabsList = this.tabsList.map((item,index)=>{
					item.name = `${item.name}(${res[index]})`;
					return item
				})
			},
		 })
	},
	// 获取订单列表
	getOrderList(){
		this.$api.post({
		    url: "/ebapi/public_api/reservation_order",
		    data: {status:this.current},
		    success: (res) => {
				this.orderList = res.map(item=>{
					item.info = JSON.parse(item.info);
					if(this.current==2){
						item.select=false;
					}
					return item
				});
				console.log(res,"res getOrderList")
			},
		 })
	},
    // 点击导航栏
    tabClick(item) {
      console.log("item", item);
      this.current = item.index;
	  this.getOrderList();
	  this.getOrderNum();
    },
    // 滑动导航栏
    swiperChange(e) {
      console.log("e", e.detail.current);
      this.current = e.detail.current;
    },
    // 触底事件
    scrolower() {},
	// 联系客服
	connection(item){
		uni.navigateTo({
			url:"/pages/my/about"
		})
	},
    // 删除订单
    del(item) {
      uni.showModal({
        title: "提示",
        content: "是否确认删除？",
        success: (res) => {
          if (res.confirm) {
			  this.$api.post({
			      url: "/ebapi/public_api/reservation_del",
			      data: {id:item.id},
			      success: (res) => {
					  // 确定
					  this.orderList.forEach((goods, index) => {
					    if (goods.id == item.id) {
							this.getOrderList();
							this.getOrderNum();
					      // this.orderList.splice(index, 1);
					    }
					  });
				  },
			   })
          } else if (res.cancel) {
            // 取消
            console.log("用户点击了取消");
          }
        },
      });
    },
    // 编辑订单
    edit(eitem) {
      let info = JSON.stringify(eitem);
      this.$uniApi.to("/pages/home/packagePrediction?info=" + info);
    },
    // 订单详情
    detail(ditem) {
      this.$uniApi.to(`/pages/home/packageDetail?id=${ditem.id}`);
    },
	claim(item){
	  this.$uniApi.to(`/pages/home/claim?info=${JSON.stringify(item)}`)
	},
    // 搜索显示
    open() {
      this.popupShow = true;
    },
    // 搜索隐藏
    close() {
      this.popupShow = false;
    },
    // 查询
    onSearch() {
      if (this.searchValue) {
      	this.popupShow = false;
        this.$uniApi.to(`/pages/search/searchDetail?title=${this.searchValue}`);
      } else {
        this.$u.toast("请输入要查询的追踪号");
      }
    },
    // 订单选择
    checkboxChange(e) {
      console.log(e, "订单选择");
	  if(e[0]){
		  for (var i = 0; i < this.orderList.length; i++) {
		  	if(this.orderList[i].id == e[0]){
		  			this.checkArrData.push(this.orderList[i]);
		  			return
		  		}
		  }
	  }else{
		  this.checkArrData = [];
	  }
	  
    },
    // 全选
    checkboxAll(e) {
      if (e[0]) {
		  this.orderList = this.orderList.map(item=>{
			  item.select = true;
			  return item;
		  })
  //       this.orderList.forEach((item) => {
  //         this.itemCheckbox.push(item.id);
  //       });
		// this.checkArrData = this.orderList;
      } else {
		  this.orderList = this.orderList.map(item=>{
		  		item.select = false;
		  		return item;
		  })
  //       this.itemCheckbox = [];
		// this.checkArrData = [];
      }
    },
    // 提交
    submit() {
	  this.checkArrData = this.orderList.filter(item=>item.select===true);
	  if(!this.checkArrData.length){
		  uni.showToast({
		  	title:"请先勾选需发货订单！",
			icon:"none"
		  })
		  return
	  }
	  uni.showModal({
	  	title: "提示",
	  	content: "可多个包裹可以一起发货哦！",
	  	success: (res) => {
			console.log(this.checkArrData,"checkArrData");
			this.$uniApi.to(`/pages/home/placeOrder?checkArrData=${JSON.stringify(this.checkArrData)}`);
		}
	  })
    },
  },
};
</script>

<style lang="scss">
page {
  padding: 0;
}

.arrived {
  .tabs-nav {
    background-color: #ffffff;
    padding-bottom: 20rpx;
  }

  .shopList {
    flex: 1;
    width: 100%;
    height: 85vh;
    overflow-y: auto;
    padding: 24rpx;

    .sl-sv {
      height: 100%;
    }

    .box {
      height: 100%;

      .item {
        // height: 433rpx;
        background: #ffffff;
        box-shadow: 0rpx 2rpx 10rpx 1rpx rgba(0, 0, 0, 0.08);
        border-radius: 16rpx 16rpx 16rpx 16rpx;
        opacity: 1;
        padding: 36rpx 36rpx 47rpx;
        margin-bottom: 24rpx;

        .title {
          font-size: $uni-text-size-title;
          color: $uni-color-main;
          font-weight: bold;
        }

        .pack {
          margin: 35rpx 0;
        }

        .time {
          margin-bottom: 35rpx;
        }

        .article {
          margin-bottom: 48rpx;
        }

        .checkbox {
          position: absolute;
		  .my-checkbox{
			  background-color: #ffffff; border:1rpx solid #c8c9cc; width: 50rpx; height: 50rpx; border-radius: 3px; 
		  }
        }

        .btnbox {
          color: $uni-text-color-grey;
          display: flex;
          justify-content: flex-end;

          .btn {
            border: 1px solid $uni-text-color-grey;
            padding: 15rpx 35rpx;
            border-radius: 5rpx;
            margin-left: 24rpx;
          }

          .detail {
            color: $uni-color-main;
            border: 1px solid $uni-color-main;
          }
		  .err {
		    color: red;
		    border: 1px solid red;
		  }
        }
      }
    }
  }

  .search {
    position: absolute;
    right: 24rpx;
    bottom: 5vh;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 96rpx;
    height: 96rpx;
    border-radius: 50%;
    background: #ffffff;
    box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(0, 0, 0, 0.16);
    opacity: 1;
  }

  .bottom {
    position: absolute;
    bottom: 0;
    width: 750rpx;
    height: 104rpx;
    padding: 0 24rpx;
    font-size: $uni-text-size-title;
    background: #ffffff;
    box-shadow: 0rpx -2rpx 0rpx 1rpx rgba(0, 0, 0, 0.16);
    opacity: 1;

    .right {
      color: #ffffff;
      background-color: $uni-color-main;
      border-radius: 35rpx;
      padding: 15rpx 38rpx;
    }
  }

  .content {
    padding: 35rpx 62rpx 50rpx;

    .ptitle {
      font-size: 36rpx;
      font-weight: bold;
      text-align: center;
      margin-top: 30rpx;
    }

    .pcontet {
      color: #999999;
      font-size: $uni-text-size-title;
      margin: 40rpx 0;
    }

    .pbtn {
      text-align: center;
      font-size: $uni-text-size-title;
      padding: 0 85rpx;

      .btn {
        padding: 18rpx 68rpx;
        border-radius: 6rpx;
      }

      .btn1 {
        border: 1px solid #999999;
      }

      .btn2 {
        background-color: $uni-color-main;
        color: #ffffff;
      }
    }
  }
}
</style>
